package org.genew.squadra.xml.model.builderfromzip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class UnzipFile {
	
	//private static List<File> listaFile;

	public static List<File> unZipIt(String zipFile,String outputFolder){
	 
    byte[] buffer = new byte[1024];
    List<File> prova = new ArrayList<File>();

    try{

   	//create output directory is not exists
   	File folder = new File(outputFolder);
   	if(!folder.exists()){
   		folder.mkdir();
   	}

   	//get the zip file content
   	ZipInputStream zis = 
   		new ZipInputStream(new FileInputStream(zipFile));
   	//get the zipped file list entry
   	ZipEntry ze = zis.getNextEntry();

   	while(ze!=null){

   	   String fileName = ze.getName();
          File newFile = new File(outputFolder + File.separator + fileName);
          
          prova.add(newFile);
        
           //create all non exists folders
           //else you will hit FileNotFoundException for compressed folder
           
           new File(newFile.getParent()).mkdirs();
           
           FileOutputStream fos = new FileOutputStream(newFile);             

           int len;
           while ((len = zis.read(buffer)) > 0) {
      		fos.write(buffer, 0, len);
           }

           fos.close();   
           ze = zis.getNextEntry();
   	}
   	
       zis.closeEntry();
   	zis.close();


   }catch(IOException ex){
      ex.printStackTrace(); 
   }
  
	return prova;	
}
	public static List<File> doUnzip(String inputZip, String destinationDirectory)
	        throws IOException {
	    int BUFFER = 2048;
	    List zipFiles = new ArrayList();
	    File sourceZipFile = new File(inputZip);
	    File unzipDestinationDirectory = new File(destinationDirectory);
	    List<File> prova = new ArrayList<File>();
	    unzipDestinationDirectory.mkdir();

	    ZipFile zipFile;
	    // Open Zip file for reading
	    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);

	    // Create an enumeration of the entries in the zip file
	    Enumeration zipFileEntries = zipFile.entries();

	    // Process each entry
	    while (zipFileEntries.hasMoreElements()) {
	        // grab a zip file entry
	        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

	        String currentEntry = entry.getName();

	        File destFile = new File(unzipDestinationDirectory, currentEntry);
	        prova.add(destFile);
	        if (currentEntry.endsWith(".zip")) {
	            zipFiles.add(destFile.getAbsolutePath());
	        }

	        // grab file's parent directory structure
	        File destinationParent = destFile.getParentFile();

	        // create the parent directory structure if needed
	        destinationParent.mkdirs();

	        try {
	            // extract file if not a directory
	            if (!entry.isDirectory()) {
	                BufferedInputStream is =
	                        new BufferedInputStream(zipFile.getInputStream(entry));
	                int currentByte;
	                // establish buffer for writing file
	                byte data[] = new byte[BUFFER];

	                // write the current file to disk
	                FileOutputStream fos = new FileOutputStream(destFile);
	                BufferedOutputStream dest =
	                        new BufferedOutputStream(fos, BUFFER);

	                // read and write until last byte is encountered
	                while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
	                    dest.write(data, 0, currentByte);
	                }
	                dest.flush();
	                dest.close();
	                is.close();
	            }
	        } catch (IOException ioe) {
	            ioe.printStackTrace();
	        }
	    }
	    zipFile.close();

	    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
	        String zipName = (String)iter.next();
	        doUnzip(
	            zipName,
	            destinationDirectory +
	                File.separatorChar +
	                zipName.substring(0,zipName.lastIndexOf(".zip"))
	        );
	    }
	    return prova;
	}
}